local _G = GLOBAL
local e_util = require "libs/entutil"
local p_util = require "libs/playerutil"


-- 阿比盖尔控制
local keybind = GetModConfigData("wendy_summonkey")
local keybind2 = GetModConfigData("wendy_commandkey")

local function InputFn(key, fn)
    if key then
        _G.TheInput:AddKeyDownHandler(key, function()
            if e_util:InGame() == "wendy" then
                local flower = p_util:GetItemFromAll("abigail_flower", nil, nil, "hands")
                if flower then
                    fn(flower)
                end
            end
        end)
    end
end

InputFn(keybind, function(flower)
    local act = p_util:GetAction("inv", "CASTSUMMON", true, flower)
    if act then
        p_util:DoAction(act, _G.RPC.ControllerUseItemOnSelfFromInvTile,act and act.action.code, flower)
    else
        local abigail = e_util:FindEnt(nil, "abigail", nil, nil, nil, nil, nil, function(ent)
            return e_util:GetLeaderTarget(ent) == _G.ThePlayer
        end)
        if abigail then
            act = p_util:GetAction("useitem", "CASTUNSUMMON", false, flower, abigail)
            p_util:DoAction(act, _G.RPC.ControllerUseItemOnSceneFromInvTile,act and act.action.code, flower, abigail)
        end
    end
end)

InputFn(keybind2, function(flower)
    local act = p_util:GetAction("inv", "COMMUNEWITHSUMMONED", true, flower)
    p_util:DoAction(act, _G.RPC.ControllerUseItemOnSelfFromInvTile, act and act.action.code, flower)
end)

local PlayerHud = require "screens/playerhud"
local old_PlayerHud_OnControl = PlayerHud.OnControl
function PlayerHud:OnControl(control, down)
    local ret = old_PlayerHud_OnControl(self, control, down)
    if control >= _G.CONTROL_INV_1 and control <= _G.CONTROL_INV_10 then
        local inventory = _G.ThePlayer.replica.inventory
        if inventory ~= nil then
            local hot_key_num = control - _G.CONTROL_INV_1 + 1
            local item = inventory:GetItemInSlot(hot_key_num)
            local abigail_flower = GetItemFromAll("abigail_flower")
            if item ~= nil and abigail_flower and item:HasTag("ghostlyelixir") then
                inventory:ControllerUseItemOnItemFromInvTile(abigail_flower,
                                                             item)
            end
        end
    end
    return ret
end

AddClassPostConstruct("widgets/invslot", function(self)
    local _UseItem = self.UseItem
    self.UseItem = function(...)
        if self.tile and self.tile.item then
            local inventory = _G.ThePlayer and _G.ThePlayer.replica.inventory or
                                  nil
            local abigail_flower = GetItemFromAll("abigail_flower")
            if inventory and abigail_flower and
                self.tile.item:HasTag("ghostlyelixir") then
                inventory:ControllerUseItemOnItemFromInvTile(abigail_flower,
                                                             self.tile.item)
            elseif inventory then
                inventory:UseItemFromInvTile(self.tile.item)
            else
                _UseItem(...)
            end
        else
            _UseItem(...)
        end
    end
end)

AddComponentPostInit("playeractionpicker", function(self)
    local old_GetInventoryActions = self.GetInventoryActions
    self.GetInventoryActions = function(self, useitem, right)
        local sorted_acts = old_GetInventoryActions(self, useitem, right)
        local abigail_flower = GetItemFromAll("abigail_flower")
        if abigail_flower and useitem:HasTag("ghostlyelixir") then
            for i, act in ipairs(sorted_acts) do
                if act.action == _G.ACTIONS.LOOKAT then
                    act.GetActionString = function()
                        return "使用"
                    end
                end
            end
        end
        return sorted_acts
    end
end)
